﻿using System;
using Exceptionless.Logging;

namespace ZilLion.Service.Core.Log
{
    /// <summary>
    ///     日志功能
    /// </summary>
    public class LoggerManager : ILoggerProvider
    {
        private static LoggerManager _instance;
        private static readonly object Instancelocker = new object();
        private ILoggerProvider _provider;

        private LoggerManager()
        {
            _provider = new DefaultLogger();
        }

   


        /// <summary>
        ///     改变默认实现
        /// </summary>
        /// <param name="provider"></param>
        public void RegisterProvider(ILoggerProvider provider)
        {
            _provider = provider;
        }


        /// <summary>
        ///     单例实例
        /// </summary>
        /// <returns></returns>
        public static LoggerManager Instance()
        {
            //没有第一重 instance == null 的话，每一次有线程进入 GetInstance()时，均会执行锁定操作来实现线程同步，
            //非常耗费性能 增加第一重instance ==null 成立时的情况下执行一次锁定以实现线程同步
            if (_instance == null)
                lock (Instancelocker)
                {
                    //Double-Check Locking 双重检查锁定
                    if (_instance == null) _instance = new LoggerManager();
                }
            return _instance;
        }

        #region interfaceImp

        public void Debug(string message, object context = null, params string[] tags)
        {
            _provider.Debug(message, context, tags);
        }

        public void Error(Exception ex, object context = null, params string[] tags)
        {
            _provider.Error(ex, context, tags);
        }

        public void Info(string message, object context = null, params string[] tags)
        {
            _provider.Info(message, context, tags);
        }

        public void Trace(string message, object context = null, params string[] tags)
        {
            _provider.Trace(message, context, tags);
        }

        public void Warn(string message, object context = null, params string[] tags)
        {
            _provider.Warn(message, context, tags);
        }

        public void Log(string message, LogLevel level, object context = null, params string[] tags)
        {
            _provider.Log(message, level, context, tags);
        }

        #endregion
    }
}